/**
 * 使用类型谓词
 * parameterName is Type
 * */

type Fish_pre = {
  name: string,
  swim: () => void
}

type Bird_pre = {
  name: string,
  fly: () => void
}

function isFish(pet: Fish | Bird): pet is Fish {
  return (pet as Fish).swim !== undefined
}

function getSmallPet(): Fish_pre | Bird_pre {
  let fish: Fish_pre = {
    name: 'shaKey',
    swim: () => {

    }
  }

  let bird: Bird_pre = {
    name: 'sparrow',
    fly: () => {

    }
  }
  return true ? fish : bird
}

let pet = getSmallPet()

if (isFish(pet)) {
  pet.swim()
} else {
  pet.fly()
}

const zoo: (Fish_pre | Bird_pre)[] = [getSmallPet(), getSmallPet(), getSmallPet()]
const underWater1: Fish_pre[] = zoo.filter(isFish)
const underWater2: Fish_pre[] = zoo.filter(isFish) as Fish_pre[]
const underWater3: Fish_pre[] = zoo.filter((pet): pet is Fish_pre => {
  if (pet.name === 'frog') {
    return false
  }
  return isFish(pet)
}) 